import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home'
import { isToken } from '../utils/index'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: '首页',
    meta: {
      needLogin: false,
      keepAlive: true
    },
    component: Home
  },
  {
    path: '/cate',
    name: '分类',
    meta: {
      needLogin: false,
      keepAlive: true
    },
    component: () => import('../views/Cate/index.vue')
  },
  {
    path: '/cart',
    name: '购物车',
    meta: {
      needLogin: true
    },
    component: () => import('../views/Cart/index.vue')
  },
  {
    path: '/user',
    name: '我的',
    meta: {
      needLogin: false
    },
    component: () => import('../views/User/index.vue')
  },
  {
    path: '/phone',
    name: '电话',
    meta: {
      needLogin: true
    },
    // 路由独享的守卫  负责拦截
    beforeEnter (to, from, next) {
      if (confirm('已满十八周岁？')) {
        // next({ path: 'home' })
        next()
      } else {
        next({ path: '/home' })
      }
    },
    component: () => import('../views/Phone/index.vue')
  },
  {
    path: '/detail',
    name: '详情',
    meta: {
      needLogin: false
    },
    component: () => import('../views/Details/index.vue')
  },
  {
    path: '/itemLists',
    name: '商品列表',
    meta: {
      needLogin: false
    },
    component: () => import('../views/ItemLists/index.vue')
  },
  {
    path: '/search',
    name: '搜索',
    meta: {
      needLogin: false
    },
    component: () => import('../views/Search/index.vue')
  },
  {
    path: '/login',
    name: '登录',
    meta: {
      needLogin: false
    },
    component: () => import('../views/Login/index.vue')
  },
  {
    path: '/register',
    name: '注册',
    meta: {
      needLogin: false
    },
    component: () => import('../views/Register/index.vue')
  },
  {
    path: '/orderCreate',
    name: '订单ye',
    meta: {
      needLogin: true
    },
    component: () => import('../views/OrderCreate/index.vue')
  },
  {
    path: '/orderCreateOne',
    name: '订单ye.',
    meta: {
      needLogin: true
    },
    component: () => import('../views/OrderCreateOne/index.vue')
  },
  {
    path: '/contactLists',
    name: '联系人列表',
    meta: {
      needLogin: true
    },
    component: () => import('../views/ContactLists/index.vue')
  },
  {
    path: '/contactListsOne',
    name: '联系人列表.',
    meta: {
      needLogin: true
    },
    component: () => import('../views/ContactListsOne/index.vue')
  },
  {
    path: '/contactAdd',
    name: '添加联系人',
    meta: {
      needLogin: true
    },
    component: () => import('../views/ContactAdd/index.vue')
  },
  {
    path: '/contactUpdate',
    name: '编辑联系人',
    meta: {
      needLogin: true
    },
    component: () => import('../views/ContactUpdate/index.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 全局守卫
router.beforeEach((to, from, next) => {
  if (to.meta.needLogin) {
    if (isToken()) {
      // 已经登陆
      next()
    } else {
      // 未登录
      next({
        // 转跳登录页并留下回来的地址
        path: '/login',
        query: {
          from: to.path
        }
      })
    }
  } else {
    next()
  }
})

export default router
